using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DiTuUI : MonoBehaviour
{
    public Button ALL_Button;
    public Button BUS_Button;  
    public Button CAT_Button;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        ALL_Button.onClick.AddListener(() => { SelectedButton(ALL_Button); ShowBiaoQian("建筑组"); });
        BUS_Button.onClick.AddListener(() => { 
            SelectedButton(BUS_Button); ShowBiaoQian("公交组", "公交路线"); 
        });
        CAT_Button.onClick.AddListener(() => { SelectedButton(CAT_Button); ShowBiaoQian("猫猫组", "猫咪活动区域"); });
        SelectedButton(ALL_Button); ShowBiaoQian("建筑组");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    /// <summary>
    /// 选择标签
    /// </summary>
    /// <param name="sb"></param>
    private void SelectedButton(Button sb)
    {
        if (sb != ALL_Button) { ALL_Button.gameObject.transform.DOLocalMoveX(420, 0.2f); ALL_Button.GetComponent<Image>().color = new Color(0.8f, 0.8f, 0.8f, 1); }
        if (sb != BUS_Button) { BUS_Button.gameObject.transform.DOLocalMoveX(420, 0.2f); BUS_Button.GetComponent<Image>().color = new Color(0.8f, 0.8f, 0.8f, 1); }
        if (sb != CAT_Button) { CAT_Button.gameObject.transform.DOLocalMoveX(420, 0.2f); CAT_Button.GetComponent<Image>().color = new Color(0.8f, 0.8f, b: 0.8f, 1); }
        sb.gameObject.transform.DOLocalMoveX(360, 0.2f);
        sb.GetComponent<Image>().color = new Color(0.3843138f, 0.1921569f, 0.937255f, 1);
    }
    /// <summary>
    /// 显示标签
    /// </summary>
    /// <param name="BiaoQianName"></param>
    private void ShowBiaoQian(string BiaoQianName)
    {
        //Camera.main.transform.DOShakePosition(0.1f, 3);//镜头抖动
        GameObject bq = GameObject.Find(BiaoQianName);
        if (bq == null)
        { 
            Debug.LogError("标签名称为" + BiaoQianName + "的物体不存在"); 
            return;
        }
        GameObject[] bqs;
        bqs = GameObject.FindGameObjectsWithTag("BiaoQian");
        foreach (GameObject b in bqs) 
        {
            if (bq == b)
            {
                DOTween.To(() => b.GetComponent<CanvasGroup>().alpha, value => b.GetComponent<CanvasGroup>().alpha = value, 1, 0.2f);
                Image[] children = b.GetComponentsInChildren<Image>();
                foreach (Image child in children)
                    child.raycastTarget = true;
            }

            else
            {
                DOTween.To(() => b.GetComponent<CanvasGroup>().alpha, value => b.GetComponent<CanvasGroup>().alpha = value, 0, 0.2f);
                Image[] children = b.GetComponentsInChildren<Image>();
                foreach (Image child in children)
                    child.raycastTarget = false;
            }

        }

    }
    //注意：标签的tag不可设置为“BiaoQian”，我们使用父物体遍历子物体的image来控制点击，如果都设置为标签就不行了。
    private void ShowBiaoQian(string BiaoQianName, string BiaoQianName2)
    {
        //镜头抖动
        //Camera.main.transform.DOShakePosition(0.1f, 3);
        GameObject bq = GameObject.Find(BiaoQianName);
        if (bq == null)
        {
            Debug.LogError("标签名称为" + BiaoQianName + "的物体不存在");
            return;
        }
        GameObject bq2 = GameObject.Find(BiaoQianName2);
        if (bq2 == null)
        {
            Debug.LogError("标签名称为" + BiaoQianName2 + "的物体不存在");
            return;
        }
        GameObject[] bqs;
        bqs = GameObject.FindGameObjectsWithTag("BiaoQian");
        foreach (GameObject b in bqs)
        {
            if (bq == b||bq2==b)
            {
                DOTween.To(() => b.GetComponent<CanvasGroup>().alpha, value => b.GetComponent<CanvasGroup>().alpha = value, 1, 0.2f);
                if (bq == b)
                {
                    Image[] children = b.GetComponentsInChildren<Image>();
                    foreach (Image child in children)
                        child.raycastTarget = true;
                }
            }

            else
            {
                DOTween.To(() => b.GetComponent<CanvasGroup>().alpha, value => b.GetComponent<CanvasGroup>().alpha = value, 0, 0.2f);
                Image[] children = b.GetComponentsInChildren<Image>();
                foreach (Image child in children)
                    child.raycastTarget = false;
            }
        }

    }
}
